package com.clock.shop.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.clock.common.enums.shop.ActivityStatus;
import com.clock.common.utils.bean.BeanUtils;
import com.clock.shop.domain.ShopActivity;
import com.clock.shop.domain.ShopShop;
import com.clock.shop.domain.vo.ActivityVo;
import com.clock.shop.domain.vo.ShopShopVo;
import com.clock.shop.mapper.ShopActivityMapper;
import com.clock.shop.mapper.ShopShopMapper;
import com.clock.shop.service.IShopActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;


/**
 * 活动信息Service业务层处理
 *
 * @author ruoyi
 * @date 2024-10-11
 */
@Service
public class ShopActivityServiceImpl extends ServiceImpl<ShopActivityMapper, ShopActivity> implements IShopActivityService {
    @Resource
    private ShopActivityMapper shopActivityMapper;

    @Resource
    private ShopShopMapper shopShopMapper;

    /**
     * 查询活动信息
     *
     * @param activityId 活动信息主键
     * @return 活动信息
     */
    @Override
    public ActivityVo selectShopActivityByActivityId(Long activityId) {
        ShopActivity shopActivity = shopActivityMapper.selectShopActivityByActivityId(activityId);
        ShopShopVo shopShop = shopShopMapper.selectShopShopByShopId(shopActivity.getShopId());
        ActivityVo activityVo = new ActivityVo();
        BeanUtils.copyProperties(shopActivity, activityVo);
        if(shopShop != null){
            activityVo.setShopName(shopShop.getShopName());
        }
        return activityVo;
    }

    /**
     * 查询活动信息列表
     *
     * @param activityVo 活动信息
     * @return 活动信息
     */
    @Override
    public List<ActivityVo> selectShopActivityList(ActivityVo activityVo) {
        // 输入店铺名为空，全查
        if (activityVo.getShopName() == null || activityVo.getShopName().equals("")  ) {
            ShopActivity shopActivity = new ShopActivity();
            BeanUtils.copyProperties(activityVo, shopActivity);
            List<ShopActivity> shopActivities = shopActivityMapper.selectShopActivityList(shopActivity);
            List<ActivityVo> activityVos = new ArrayList<>();
            for (ShopActivity s : shopActivities
            ) {
                ActivityVo activityVo1 = new ActivityVo();
                BeanUtils.copyProperties(s, activityVo1);
                // 根据店铺id查询店铺名称
                ShopShopVo shopShopVo = shopShopMapper.selectShopShopByShopId(s.getShopId());
                if (shopShopVo != null) {
                    String shopName = shopShopVo.getShopName();
                    activityVo1.setShopName(shopName);
                }
                // 设置活动状态
                if(s.getActivityStatus().equals(ActivityStatus.GOING.getCode())){
                    activityVo1.setActivityStatus(ActivityStatus.GOING.getInfo());
                }else {
                    activityVo1.setActivityStatus(ActivityStatus.ENDING.getInfo());
                }
                activityVos.add(activityVo1);
            }
            return activityVos;
        } else {
            List<ShopShop> shopShops = shopShopMapper.selectShopShopByShopName(activityVo.getShopName());
            ShopActivity shopActivity = new ShopActivity();
            BeanUtils.copyProperties(activityVo, shopActivity);
            shopActivity.setShopId(shopShops.get(0).getShopId());
            List<ShopActivity> shopActivities = shopActivityMapper.selectShopActivityList(shopActivity);
            List<ActivityVo> activityVos = new ArrayList<>();
            for (ShopActivity s : shopActivities
            ) {
                ActivityVo activityVo1 = new ActivityVo();
                BeanUtils.copyProperties(s, activityVo1);
                activityVo1.setShopName(activityVo.getShopName());
                activityVos.add(activityVo1);
                // 设置活动状态
                if(s.getActivityStatus().equals(ActivityStatus.GOING.getCode())){
                    activityVo1.setActivityStatus(ActivityStatus.GOING.getInfo());
                }else {
                    activityVo1.setActivityStatus(ActivityStatus.ENDING.getInfo());
                }
            }
            return activityVos;
        }
    }

    /**
     * 新增活动信息
     *
     * @param activityVo 活动信息
     * @return 结果
     */
    @Override
    public int insertShopActivity(ActivityVo activityVo) {
        List<ShopShop> shopShops = shopShopMapper.selectShopShopByShopName(activityVo.getShopName());
        ShopActivity shopActivity = new ShopActivity();
        BeanUtils.copyProperties(activityVo, shopActivity);
        shopActivity.setShopId(shopShops.get(0).getShopId());
        return shopActivityMapper.insertShopActivity(shopActivity);
    }

    /**
     * 修改活动信息
     *
     * @param shopActivity 活动信息
     * @return 结果
     */
    @Override
    public int updateShopActivity(ShopActivity shopActivity) {
        return shopActivityMapper.updateShopActivity(shopActivity);
    }

    /**
     * 批量删除活动信息
     *
     * @param activityIds 需要删除的活动信息主键
     * @return 结果
     */
    @Override
    public int deleteShopActivityByActivityIds(Long[] activityIds) {
        return shopActivityMapper.deleteShopActivityByActivityIds(activityIds);
    }

    /**
     * 删除活动信息信息
     *
     * @param activityId 活动信息主键
     * @return 结果
     */
    @Override
    public int deleteShopActivityByActivityId(Long activityId) {
        return shopActivityMapper.deleteShopActivityByActivityId(activityId);
    }
}
